<?php
	if (!empty($search)) {
		echo '<div class="content_header">';
		echo __d('lil_blogs', 'Search Results', true);
		echo '</div>';
	} else {
		$this->set('title_for_layout', $this->Html->clean($blog['Blog']['name']));
	}
?>
<div id="post-index">
<?php if (is_array($posts)) foreach ($posts as $post) { ?>
<div class="post" id="post-<?php echo $post['Post']['id']; ?>">
	<h1>
	<?php
		echo $this->Html->link($post['Post']['title'], array(
			'plugin'     => 'lil_blogs',
			'controller' => 'posts',
			'action'     => 'view',
			'blog'       => $blog['Blog'][Configure::read('LilBlogs.blogSlugField')],
			'post'       => $post['Post']['slug']));
	?>
	</h1>
	<div class="meta">
		<?php

			if (!empty($post['Author'][Configure::read('Lil.authorDisplayField')])) {
				printf(
					__d('lil_blogs', 'Posted %1$s by %2$s', true),
					$this->Time->timeAgoInWords($post['Post']['created']),
					$post['Author'][Configure::read('Lil.authorDisplayField')]
				);
			} else {
				printf(
					__d('lil_blogs', 'Posted %s', true),
					$this->Time->timeAgoInWords($post['Post']['created'])
				);
			}
			
			// display list of categories
			if (!empty($post['Category'])) {
				echo ' ' . __d('lil_blogs', 'in', true) . ' ';
				
				echo $this->Text->toList(
					Set::format(
						$post, 
						$this->Html->link('%1$s', array(
							'controller' => 'posts',
							'action'     => 'index',
							'blog'       => $blog['Blog'][Configure::read('LilBlogs.blogSlugField')],
							'category'   => '%2$s'
						)), 
						array('Category.{n}.name', 'Category.{n}.id')
					),
					__d('lil_blogs', 'and', true)
				);
			}
			
			// show no of comments
			if (Configure::read('LilBlogs.commentAssociation')) {
				echo ' (';
				// this is neccessary because of i18n extraction requires variable
				$comment_count = $post['Post']['no_comments'];
				echo $this->Html->link(
					sprintf(
						__dn('lil_blogs', '1 comment', '%d comments', $comment_count, true), $comment_count
					),
					array(
						'plugin'     => 'lil_blogs',
						'controller' => 'posts',
						'action'     => 'view',
						'blog'       => $blog['Blog'][Configure::read('LilBlogs.blogSlugField')],
						'post'       => $post['Post']['slug'] . '#comments',
					)
				);
				echo ')';
			}
			
			// show "edit this post" link
			if (isset($this->Lil) && $this->Lil->user('id')) {
				echo ' | ';
				echo $this->Html->link(
					__d('lil_blogs', 'Edit this post', true),
					array(
						'admin'  => true,
						'action' => 'edit',
						$post['Post']['id']
					)
				);
			}
		?>
	</div>
	<div class="body"><?php
		$body = $post['Post']['body'];
		
		if ($excerpt_length = Configure::read('LilBlogs.excerptLength')) {
			$body = $this->Text->truncate(
				$body, 
				$excerpt_length,
				array(
					'ending' => '...',
					'exact'  => false,
					'html'   => true
				)
			);
		} else if (stripos($body, Configure::read('LilBlogs.excerptDelimiter')) !== false) {
			$body = substr($body, 0, stripos($body, Configure::read('LilBlogs.excerptDelimiter')));
		}
		echo $this->LilBlogs->autop($body);
	?></div>
	<?php
		if (strlen($body) != strlen($post['Post']['body'])) {
	?>
	<div class="more">
		<?php
			echo $this->Html->link(__d('lil_blogs', 'Read more', true), array(
				'plugin'     => 'lil',
				'controller' => 'posts',
				'action'     => 'view',
				'blog'       => $blog['Blog'][Configure::read('LilBlogs.blogSlugField')],
				'post'       => $post['Post']['slug'])); ?>
	</div>
	<?php
		}
	?>
</div>
<?php }  // foreach ($posts as $post) ?>
<div class="paging">
<?php
	echo $this->Paginator->next(__d('lil_blogs', 'Older', true), array('url' => array('blog' => $blog['Blog'][Configure::read('LilBlogs.blogSlugField')])), '');
	echo $this->Paginator->prev(__d('lil_blogs', 'Newer', true), array('url' => array('blog' => $blog['Blog'][Configure::read('LilBlogs.blogSlugField')])), '');
?>
</div>
</div>